package net.zhengxinyun.performance.controller.department;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.DepartmentEntity;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.DepartClassFilter;
import net.zhengxinyun.performance.filter.DepartmentFilter;
import net.zhengxinyun.performance.service.departClass.DepartClassService;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 部门管理控制层
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    private DepartmentService departmentService;
    private LoginService loginService;
    private TokenService tokenService;
    private DepartClassService departClassService;

    @Autowired
    public DepartmentController(DepartmentService departmentService, LoginService loginService, TokenService tokenService,DepartClassService departClassService) {
        this.departmentService = departmentService;
        this.loginService = loginService;
        this.tokenService = tokenService;
        this.departClassService=departClassService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody DepartmentParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){


           // sys

            String str = departmentService.insert(param.departmentEntity);
            if (!"添加成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody DepartmentParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = departmentService.update(param.departmentEntity);
            if (!"修改成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody DepartmentParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = departmentService.delete(param.departmentEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对");
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody DepartmentParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            List<DepartmentFilter> list = departmentService.query(param.departmentFilter);
             if (list.size() == 0 && param.departmentFilter.getPageNumber() != 0){
                param.departmentFilter.setPageNumber(param.departmentFilter.getPageNumber()-1);
            }

            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
             if(!"admin".equals(userEntity.getUserPermissions()))
             {
                 param.departmentFilter.setDepartcode(userEntity.getDepartcode());
             }

            list = departmentService.query(param.departmentFilter);
            Integer pageTotal = departmentService.queryForCount(param.departmentFilter);
            map.put("list",list);
//            map.put("pageTotal",pageTotal);
            //获取单位分类
            DepartClassFilter filter  = new DepartClassFilter();
           // HashMap<String,Object> classmap = new HashMap<>();
            List<DepartClassFilter> classlists=departClassService.query(filter);
            map.put("departclssList",classlists);

            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对",null);
    }

    @PostMapping("/batchUpdateClass")
    public Result<String> batchUpdateClass(@RequestBody DepartmentParam param)
    {
        if (tokenService.compareToken(param.tokenEntity.getValue())){

            List<Long> listId = param.departmentEntity.getListId();
            if(ObjectUtils.isBlank(listId))
            {
                return  ResultUtils.getResults(Result.Status.ERROR,"请勾选要批量修改的部门");
            }
            String classId  = param.departmentEntity.getDepartClas();
            DepartmentEntity departmentEntity = new DepartmentEntity();
            for (Long aLong : listId) {
                departmentEntity.setId(aLong);
                departmentEntity.setDepartClas(classId);
                departmentService.update(departmentEntity);
            }

            return ResultUtils.getSuccessResults();

        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token错误",null);
    }


}

class DepartmentParam{
    public DepartmentFilter departmentFilter;
    public DepartmentEntity departmentEntity;
    public TokenEntity tokenEntity;
}
